package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Details;
import cn.wolfcode.crm.domain.Directory;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDetailsService;
import cn.wolfcode.crm.service.IDirectoryService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("dictionaries")
public class DirectoryController {
    /*dictionaries/view.do*/
    @Autowired
    private IDirectoryService directoryService;
    @Autowired
    private IDetailsService detailsService;

    @Autowired
    @RequestMapping("view")
    public String index() {
        return "dictionaries";
    }

    @RequestMapping("getRoleIdByEmpId")
    @ResponseBody
    public List<Details> getRoleIdByEmpId(String dictionariesSn) {
        return detailsService.getDictionariesSnByDetails(dictionariesSn);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Directory> selectAll() {
        return directoryService.selectAll();
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"dictionaries:query", "字典列表"}, logical = Logical.OR)
    public PageResult query(QueryObject qo) {
        return directoryService.query(qo);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"dictionaries:saveOrUpdate", "字典新增"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Directory dept) {
        try {
            directoryService.insert(dept);
            return JsonResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("保存失败");
        }
    }

    @RequestMapping("getDictionariesSnByDetails")
    @ResponseBody
    public List<Details> getDictionariesSnByDetails(String sn) {
        return detailsService.getDictionariesSnByDetails(sn);
    }

    @RequestMapping("getPlanDetails")
    @ResponseBody
    public List<Details> getPlanDetails(String sn) {
        return detailsService.getPlanDetails(sn);
    }
}
